#!/bin/bash
# 设置工作目录为项目根目录
WORKDIR="$(dirname "$(dirname "$(readlink -f "$0")")")"
cd "$WORKDIR" || exit 1

# 从环境变量获取配置参数
export MEDIA_SERVICE_CONFIG=${MEDIA_SERVICE_CONFIG:-"./configs/config.json"}
export WORKERS=${WORKERS:-4}  # 默认4个worker
export GUNICORN_BIND=${GUNICORN_BIND:-"0.0.0.0:5555"}
export LOG_LEVEL=${LOG_LEVEL:-"info"}
export RELOAD_FLAG=${RELOAD_FLAG:-""}

# 创建日志目录
LOG_DIR=${LOG_DIR:-"/home/bhrm/logs"}
mkdir -p "$LOG_DIR"

# 定义日志文件名
LOG_FILE="$LOG_DIR/flask-run-logs_$(date +%Y-%m-%d).log"

# 使用nohup运行gunicorn
nohup gunicorn -w $WORKERS -b $GUNICORN_BIND $RELOAD_FLAG --log-level $LOG_LEVEL \
'run_with_gunicorn:app' >> "$LOG_FILE" 2>&1 &

echo "Gunicorn started with PID $!, logs written to $LOG_FILE"
echo "Configuration:"
echo "  Workers: $WORKERS"
echo "  Bind: $GUNICORN_BIND"
echo "  Log Level: $LOG_LEVEL"
echo "  Reload: ${RELOAD_FLAG:-disabled}"
